package com.food.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.food.entity.LayuiR;
import com.food.entity.po.FoodApiGd;
import com.food.entity.po.FoodApiBaidu;
import com.food.entity.po.FoodApiPhoto;
import com.food.entity.po.FoodApiTx;
import com.food.entity.vo.*;
import com.food.mapping.FoodShopMapper;
import com.food.service.FoodShopService;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @author : 港某
 * @date : 2021/1/8 14:31
 */
@Service
public class FoodShopServiceImpl extends ServiceImpl<FoodShopMapper, Object> implements FoodShopService {

    @Override
    public Date getDatabaseTime() {
        return baseMapper.getDatabaseTime();
    }

    @Override
    public void insertFoodShop(FoodApiGd foodApiGd) {
        baseMapper.insertFoodShop(foodApiGd);
    }

    @Override
    public void insertFoodShopPhoto(FoodApiPhoto foodApiPhoto) {
        baseMapper.insertFoodShopPhoto(foodApiPhoto);
    }

    @Override
    public LayuiR<FoodApiGd> getFoodShop(FoodShopSearch foodShopSearch) {
        Page<FoodApiGd> page = new Page<>(foodShopSearch.getPage(), foodShopSearch.getLimit());
        List<FoodApiGd> foodApiGd = baseMapper.getFoodShop(page, foodShopSearch);
        page.setRecords(foodApiGd);
        return new LayuiR<>(page);
    }

    @Override
    public LayuiR<FoodApiBaidu> getFoodShopBaidu(FoodShopSearch foodShopSearch) {
        Page<FoodApiBaidu> page = new Page<>(foodShopSearch.getPage(), foodShopSearch.getLimit());
        List<FoodApiBaidu> foodShop = baseMapper.getFoodShopBaidu(page, foodShopSearch);
        page.setRecords(foodShop);
        return new LayuiR<>(page);
    }

    @Override
    public LayuiR<FoodApiTx> getFoodShopTx(FoodShopSearch foodShopSearch) {
        Page<FoodApiTx> page = new Page<>(foodShopSearch.getPage(), foodShopSearch.getLimit());
        List<FoodApiTx> foodShop = baseMapper.getFoodShopTx(page, foodShopSearch);
        page.setRecords(foodShop);
        return new LayuiR<>(page);
    }

    @Override
    public List<Pie> getPieCount1() {
        return baseMapper.getPieCount1();
    }

    @Override
    public List<Pie> getPieCount2() {
        return baseMapper.getPieCount2();
    }

    @Override
    public List<Pie> getPieCount3() {
        return baseMapper.getPieCount3();
    }

    @Override
    public List<PieDouble> getPieCount4() {
        return baseMapper.getPieCount4();
    }

    @Override
    public List<PieDouble> getPieCount5() {
        return baseMapper.getPieCount5();
    }

    @Override
    public List<Pie> getPieCount6() {
        return baseMapper.getPieCount6();
    }

    @Override
    public List<PieDouble> getPieCount7() {
        return baseMapper.getPieCount7();
    }

    @Override
    public List<Pie> getBarCount1() {
        return baseMapper.getBarCount1();
    }

    @Override
    public List<Bar> getBarCount2() {
        return baseMapper.getBarCount2();
    }

    @Override
    public List<HeatMap> getHeatMap() {
        List<HeatMap> heatMaps = new LinkedList<>();
        List<HeatMap> heatMap = baseMapper.getHeatMap();
        for (HeatMap map : heatMap) {
            if (map.getCount() > 0 && map.getLng() > 0 && map.getLat() > 0) {
                heatMaps.add(map);
            }
        }
        return heatMaps;
    }

    @Override
    public List<Sun> getSunCount1() {
        List<Map<String, String>> sunCount1 = baseMapper.getSunCount1();
        return parseSun(sunCount1);
    }

    @Override
    public List<Sun> getSunCount2() {
        List<Map<String, String>> sunCount2 = baseMapper.getSunCount2();
        return parseSun(sunCount2);
    }

    /**
     * 解析sun图
     */
    private List<Sun> parseSun(List<Map<String, String>> mapList) {
        List<Sun> sunList = new LinkedList<>();
        for (Map<String, String> stringStringMap : mapList) {
            String name = stringStringMap.get("name");
            String child = stringStringMap.get("child");
            String value = stringStringMap.get("value");
            Sun tmp = new Sun(name);
            if (sunList.size() == 0) {
                sunList.add(tmp);
                tmp.getChildren().add(new Sun(child, Double.parseDouble(value)));
            } else {
                int i = 0;
                for (Sun sun : sunList) {
                    if (sun.getName().equals(name)) {
                        sun.getChildren().add(new Sun(child, Double.parseDouble(value)));
                        break;
                    }
                    i++;
                }
                if (i == sunList.size()) {
                    sunList.add(tmp);
                    tmp.getChildren().add(new Sun(child, Double.parseDouble(value)));
                }
            }
        }
        return sunList;
    }

}
